package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.Proscenium;
import com.langhua.service.ProsceniumService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/prosceniums")
public class ProsceniumController {

    @Autowired
    private ProsceniumService prosceniumService;


    /**
     * 采购员登录
     * @param request
     * @param proscenium
     * @return
     */
    @PostMapping("/login")
    public R<Proscenium> login(@RequestBody Proscenium proscenium){

        System.out.println("proscenium+login:"+proscenium);

        //2、根据页面提交的前台名username查询数据库
        LambdaQueryWrapper<Proscenium> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Proscenium::getUsername,proscenium.getUsername());
        Proscenium emp = prosceniumService.getOne(queryWrapper);

        System.out.println("proscenium+登录搜索结果:"+emp);

        //3、如果没有查询到则返回登录失败结果
        if(emp == null){
            return R.error("登录失败！无此账号！");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if(!emp.getPassword().equals(proscenium.getPassword())){
            return R.error("登录失败!密码错误！");
        }

        //5、查看采购员状态，如果为已禁用状态，则返回采购员已禁用结果
        if("false".equals(emp.getStatus())){
            return R.error("账号已禁用");
        }

        return R.success(emp,"登录成功!");
    }

    /**
     * 采购员退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("proscenium");
        return R.success("退出成功",null);
    }

    /**
     * 采购员信息展示
     * @param current  当前页
     * @param size     每页条数
     * @param proscenium   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current,@PathVariable Integer size,Proscenium proscenium){

        IPage page=new Page(current,size);

        LambdaQueryWrapper<Proscenium> lqw=new LambdaQueryWrapper<Proscenium>();
        lqw.like(Strings.isNotEmpty(proscenium.getName()),Proscenium::getName,proscenium.getName());

        IPage page1 = prosceniumService.page(page,lqw);

        System.out.println("前台分页查询内容："+page1);
        return R.success(page1,"proscenium分页查询");
    }
    /**
     * 新增采购员
     * @param proscenium
     * @return
     */
    @PostMapping("/add")
    public R<String> save(HttpServletRequest request,@RequestBody Proscenium proscenium){
        log.info("新增采购员，采购员信息：{}",proscenium.toString());

        boolean saveProscenium = prosceniumService.save(proscenium);
        if (saveProscenium){
            R.error("新增采购员失败");
        }

        return R.success("新增采购员成功",null);
    }


    /**
     * 根据id查询采购员信息 (修改数据回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Proscenium> getById(@PathVariable Long id){
        log.info("根据id查询采购员信息..."+id);
        Proscenium proscenium = prosceniumService.getById(id);
        if(proscenium != null){
            return R.success(proscenium,null);
        }
        return R.error("没有查询到对应采购员信息");
    }

    /**
     * 根据id修改采购员信息()
     * @param proscenium
     * @return
     */
    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Proscenium proscenium){
        log.info(proscenium.toString());

        long id = Thread.currentThread().getId();
        log.info("修改id为：{}",id);
        prosceniumService.updateById(proscenium);

        return R.success(proscenium,"采购员信息修改成功");
    }

    /**
     * 通过id删除Proscenium
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除Proscenium的id为：{}", id);

        prosceniumService.removeById(id);
        return R.success(null,"删除成功！");
    }

    /**
     * 通过id修改前台账号状态
     * @return
     */
    @PutMapping("/updateProsceniumStatus")
    public R updateProsceniumStatus(@RequestBody Proscenium proscenium){
        boolean b = prosceniumService.updateById(proscenium);
        if (b){
            return R.success(proscenium,"通过id修改前台账号状态成功");
        }else {
            return R.error("通过id修改前台账号状态失败！");
        }
    }

}
